package pack.pattern;

import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.util.Linkify;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class PatternActivity extends Activity {
    private final Handler mHandler=new Handler();
    protected static final String DONATION_URI="market://details?id=pack.patternsdonation";
    private static final int SCREEN_NUMBER=7;
    protected static final long WAIT_MILLS=550;
    private final Integer[] mImageIds={R.drawable.im00, R.drawable.im01, R.drawable.im02, R.drawable.im03,
	R.drawable.im04, R.drawable.im05, R.drawable.im06, R.drawable.im07, R.drawable.im08, R.drawable.im09,
	R.drawable.im10, R.drawable.im11, R.drawable.im12, R.drawable.im13, R.drawable.im14, R.drawable.im15,
	R.drawable.im16, R.drawable.im17, R.drawable.im18, R.drawable.im19, R.drawable.im20, R.drawable.im21,
	R.drawable.im22, R.drawable.im23, R.drawable.im24, R.drawable.im25, R.drawable.im26, R.drawable.im27,
	R.drawable.im28, R.drawable.im29, R.drawable.im30, R.drawable.im31, R.drawable.im32, R.drawable.im33,
	R.drawable.im34, R.drawable.im35, R.drawable.im36, R.drawable.im37, R.drawable.im38, R.drawable.im39,
	R.drawable.im40, R.drawable.im41, R.drawable.im42, R.drawable.im43, R.drawable.im44, R.drawable.im45,
	R.drawable.im46, R.drawable.im47, R.drawable.im48, R.drawable.im49, R.drawable.im50, R.drawable.im51,
	R.drawable.im52, R.drawable.im53, R.drawable.im54, R.drawable.im55, R.drawable.im56, R.drawable.im57,
	R.drawable.im58, R.drawable.im59, R.drawable.im60, R.drawable.im61, R.drawable.im62, R.drawable.im63,
	R.drawable.im64, R.drawable.im65, R.drawable.im66, R.drawable.im67, R.drawable.im68, R.drawable.im69,
	R.drawable.im70, R.drawable.im71, R.drawable.im72, R.drawable.im73, R.drawable.im74, R.drawable.im75,
	R.drawable.im76, R.drawable.im77, R.drawable.im78, R.drawable.im79, R.drawable.im80, R.drawable.im81,
	R.drawable.im82, R.drawable.im83, R.drawable.im84, R.drawable.im85 };
    private int position=0;
    private boolean starting=true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	Gallery gallery=(Gallery) findViewById(R.id.gallery);
	gallery.setAdapter(new ImageAdapter(this));
	gallery.setOnItemClickListener(new OnItemClickListener() {
	    @Override
	    public void onItemClick(AdapterView parent, View v, int position, long id) {
		previewWallpaper(position);
	    }
	});

	ImageButton setBtn=(ImageButton) findViewById(R.id.buttonSet);
	setBtn.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		if ( ! starting) {
		    Toast.makeText(PatternActivity.this, getResources().getString(R.string.toast_confirm),
			Toast.LENGTH_LONG).show();
		    mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
			    createWallpaper(position);
			}
		    }, WAIT_MILLS);
		}
		else {
		    Toast.makeText(PatternActivity.this, getResources().getString(R.string.toast_select),
			Toast.LENGTH_LONG).show();
		}

	    }
	});

	ImageButton infoBtn=(ImageButton) findViewById(R.id.buttonInfo);
	infoBtn.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		showCredits();
	    }
	});

    }

    public class ImageAdapter extends BaseAdapter {
	int mGalleryItemBackground;
	private final Context mContext;

	public ImageAdapter(Context c) {
	    mContext=c;
	    TypedArray attr=mContext.obtainStyledAttributes(R.styleable.Gallery);
	    mGalleryItemBackground=attr.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
	    attr.recycle();
	}

	@Override
	public int getCount() {
	    return mImageIds.length;
	}

	@Override
	public Object getItem(int position) {
	    return position;
	}

	@Override
	public long getItemId(int position) {
	    return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
	    ImageView imageView=new ImageView(mContext);
	    imageView.setImageResource(mImageIds[position]);
	    imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
	    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
	    imageView.setBackgroundResource(mGalleryItemBackground);
	    return imageView;
	}
    }

    private Bitmap generateBitmap(int pos, int width, int height) {
	Bitmap tileImage=BitmapFactory.decodeResource(getResources(), mImageIds[pos]);
	BitmapDrawable backWave=new BitmapDrawable(getResources(), tileImage);
	backWave.setTileModeX(TileMode.REPEAT);
	backWave.setTileModeY(TileMode.REPEAT);
	backWave.setBounds(0, 0, width, height);
	Bitmap bitmap=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
	Canvas c2=new Canvas(bitmap);
	backWave.draw(c2);
	return bitmap;
    }

    private void previewWallpaper(int pos) {
	ImageView img=(ImageView) findViewById(R.id.imageView);
	Bitmap bitmap=generateBitmap(pos, img.getWidth(), img.getHeight());
	img.setImageBitmap(bitmap);
	if (starting) {
	    // img.setBackgroundColor(getResources().getColor(R.color.dark_grey));
	    img.setPadding(15, 15, 15, 15);
	    starting=false;
	}
	position=pos;
    }

    private void createWallpaper(int pos) {
	Display display=getWindowManager().getDefaultDisplay();
	Bitmap bitmap=generateBitmap(position, display.getWidth() * SCREEN_NUMBER, display.getHeight());
	WallpaperManager wallpaperManager=WallpaperManager.getInstance(this);
	try {
	    wallpaperManager.setBitmap(bitmap);
	}
	catch (IOException e) {
	    // e.printStackTrace();
	}
    }

    private void showCredits() {
	// Info
	AlertDialog.Builder builder=new AlertDialog.Builder(this);
	builder.setTitle(R.string.info_title);
	builder.setCancelable(true);
	final SpannableString s=new SpannableString(getText(R.string.info_text));
	Linkify.addLinks(s, Linkify.WEB_URLS);
	builder.setMessage(s);
	builder.setPositiveButton(R.string.info_btn_donation, new DialogInterface.OnClickListener() {
	    @Override
	    public void onClick(DialogInterface dialog, int id) {
		Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(DONATION_URI));
		startActivity(i);
	    }
	});
	builder.setIcon(R.drawable.ic_launcher);
	builder.show();
    }
}